﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LogisticsDAL;
using System.Data.SqlClient;
using System.Data;
using Model;

namespace LogisticsBLL
{
    public class DriverBLL
    {
        //驾驶员绑定
        public List<Drivers> BindDriver(int pageIndex, int pageSize, out int count,Drivers drs)
        {
            DriverDAL dr = new DriverDAL();
            return dr.Binddriver(pageIndex, pageSize, out count,drs);
        }

        /// <summary>
        /// 车队名称下拉列表绑定
        /// </summary>
        /// <returns></returns>
        public DataTable ddlbindCarName()
        {
            TruckDAL tru = new TruckDAL();
            return tru.ddlbindCarTeam();
        }

        public DataTable selDriver(string Name, string Sex, string phone, string bRith, string TeamNaem, string IsDelete, string IDcard, string checkTime)
        {
            DriverDAL da = new DriverDAL();
            //List<Drivers> drlist = da.SelectDriver(Name, Sex, phone, bRith, TeamNaem, State, IDcard, checkTime);

            return da.SelectDriver(Name, Sex, phone, bRith, TeamNaem, IsDelete, IDcard, checkTime);
        }

        /// <summary>
        /// 添加驾驶员信息
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="sex"></param>
        /// <param name="brith"></param>
        /// <param name="phone"></param>
        /// <param name="idcard"></param>
        /// <param name="truckid"></param>
        /// <param name="state"></param>
        /// <param name="remark"></param>
        /// <param name="checktime"></param>
        /// <returns></returns>
        public int AddDriver(string Name, string sex, string brith, string phone, string idcard, string truckid, string state, string remark, string checktime)
        {
            DriverDAL da = new DriverDAL();
            return da.AddDrivers(Name, sex, brith, phone, idcard, truckid, state, remark, checktime);
        }

        /// <summary>
        /// 删除驾驶员信息
        /// </summary>
        /// <param name="drid"></param>
        /// <returns></returns>
        public string dropDriver(int drid)
        {
            DriverDAL dda = new DriverDAL();
            string str = "";
            if (dda.GetDriversDId(drid) <= 0)
            {
                str="没有该驾驶员";
            }
            else if(dda.GetContactDId(drid)>=1)
            {
                str="该驾驶员和车辆绑定了,不能删除";
            }
            else
            {
                int result = dda.delDriver(drid);
                if (result > 0)
                {
                    str = "删除成功";
                }
            }
            return str;
        }

        public Drivers UpBindDriver(int drids)
        {
            DriverDAL dal = new DriverDAL();
            return dal.UpBindDriver(drids);
        }


        public int UpdateDriver(Drivers drv)
        {
            DriverDAL dda = new DriverDAL();
            return dda.updateDriver(drv);
        }

        /// <summary>
        /// 解除绑定
        /// </summary>
        /// <param name="drid"></param>
        /// <returns></returns>
        public string lingbind(int drid)
        {
            DriverDAL dda = new DriverDAL();
            string str = "";
            if (dda.sqlealerDriverState(drid) != 1)
            {
                str = "驾驶员正在承运中不能解绑！";
            }
            if (dda.sqlealerTruckState(drid) != 0)
            {
                str = "车辆正在承运中不能解绑！";
            }

           
                int result = dda.unbound(drid);
                if (result > 0)
                {
                    str = "解绑成功！";
                } 
            
            return str;
        }


        /// <summary>
        /// 绑定车辆列表信息
        /// </summary>
        /// <returns></returns>
        public DataTable bindturkc(int drid, int pageIndex, int pageSize, out int count)
        {
            DriverDAL ddl = new DriverDAL();
            return ddl.bindtruck(drid,pageIndex,pageSize,out count);
        }

        public string bindtruckdriver(int truckID,int DriverID)
        {
            DriverDAL drdal = new DriverDAL();
            int result = drdal.insetBindTruck(truckID, DriverID);
            string str = "";
            if (result > 0)
            {
                str = "绑定成功";
            }
            else
            {
                str = "绑定失败";
            }
            return str;
        }

        
       
    }
}
